<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserProfileById } from '@/api/user'
import { assignRoles } from '@/api/employees'
import { getRoles } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      roleIds: [],
      list: [],
      pageSize: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 更改权限
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染列表
        this.loadRoles()
        // 关闭弹层
        this.$emit('close')
        console.log('assignRoles', res)
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    },

    // 获取权限列表
    async loadRoles() {
      try {
        // 渲染复选框
        const res = await getRoles({ pageSize: 100 })
        this.list = res.data.rows
        // 发送请求获取当前拥有的角色权限，选中复选框
        const info = await getUserProfileById(this.roleId)
        console.log('getUserProfileById', res)
        this.roleIds = info.data.roleIds

        // 保存数据
      } catch (err) {
        console.log('getUserProfileById', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>

